<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>创建一个表示数据库连接的 PDO 实例</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdo.commit.html">PDO::commit</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdo.errorcode.html">PDO::errorCode</a></div>
 <div class="up"><a href="class.pdo.html">PDO</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="pdo.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::__construct</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDO::__construct</span> &mdash; <span class="dc-title">
    创建一个表示数据库连接的 PDO 实例
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdo.construct-description">
  <h3 class="title">说明</h3>
  <div class="constructorsynopsis dc-description">
   <span class="methodname"><strong>PDO::__construct()</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$dsn</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$username</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$driver_options</code></span>
  ]]] )</div>

  <p class="para rdfs-comment">
    创建一个表示连接到请求数据库的数据库连接 PDO 实例。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-pdo.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    <dt>
dsn</dt>

     <dd>

      <p class="para">
       数据源名称或叫做 DSN，包含了请求连接到数据库的信息。
      </p>
      <p class="para">
       通常，一个 DSN 由 PDO 驱动名、紧随其后的冒号、以及具体 PDO 驱动的连接语法组成。更深入的信息能从 <a href="pdo.drivers.html" class="link">PDO 具体驱动文档</a>找到。
      </p>
      <p class="para">
       The <em><code class="parameter">dsn</code></em> 参数支持三种不同的方式 创建一个数据库连接：
      </p>
      <p class="para">
       <dl>

        <dt>
Driver invocation</dt>

         <dd>

          <p class="para">
           <em><code class="parameter">dsn</code></em> 包含完整的DSN。
          </p>
         </dd>

        
        <dt>
URI invocation</dt>

         <dd>

          <p class="para">
           <em><code class="parameter">dsn</code></em> consists of <strong class="userinput"><code>uri:</code></strong>
           followed by a URI that defines the location of a file containing
           the DSN string. The URI can specify a local file or a remote URL.
          </p>
          <p class="para"><strong class="userinput"><code>uri:file:///path/to/dsnfile</code></strong></p>
         </dd>

        
        <dt>
Aliasing</dt>

         <dd>

          <p class="para">
           <em><code class="parameter">dsn</code></em> consists of a name 
           <em><code class="parameter">name</code></em> that maps to 
           <code class="code">pdo.dsn.<em><code class="parameter">name</code></em></code> in <var class="filename">php.ini</var>
           defining the DSN string.
          </p>
          <blockquote class="note"><p><strong class="note">Note</strong>: 
           <p class="para">
             别名必须得在 <var class="filename">php.ini</var> 中定义了，不能是在 <var class="filename">.htaccess</var> 或 <var class="filename">httpd.conf</var> 中 。
           </p>
          </p></blockquote>
         </dd>

        
       </dl>

      </p>
     </dd>

    
    <dt>
username</dt>

     <dd>

      <p class="para">
       DSN字符串中的用户名。对于某些PDO驱动，此参数为可选项。
      </p>
     </dd>

    
    <dt>
password</dt>

     <dd>

      <p class="para">
       DSN字符串中的密码。对于某些PDO驱动，此参数为可选项。
      </p>
     </dd>

    
    <dt>
driver_options</dt>

     <dd>

      <p class="para">
       一个具体驱动的连接选项的键=&gt;值数组。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdo.construct-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功则返回一个PDO对象。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-pdo.construct-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果试图连接到请求的数据库失败，则<span class="function"><strong>PDO::__construct()</strong></span> 抛出一个 PDO异常（<a href="class.pdoexception.html" class="classname">PDOException</a>）  。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo.construct-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-892"><p><strong>Example #1 Create a PDO instance via driver invocation</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Connect&nbsp;to&nbsp;an&nbsp;ODBC&nbsp;database&nbsp;using&nbsp;driver&nbsp;invocation&nbsp;*/<br /></span><span style="color: #0000BB">$dsn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'mysql:dbname=testdb;host=127.0.0.1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'dbuser'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'dbpass'</span><span style="color: #007700">;<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">PDOException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Connection&nbsp;failed:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-893"><p><strong>Example #2 Create a PDO instance via URI invocation</strong></p>
    <div class="example-contents"><p>
     The following example assumes that the file
     <var class="filename">/usr/local/dbconnect</var> exists with file permissions
     that enable PHP to read the file. The file contains the PDO DSN to
     connect to a DB2 database through the PDO_ODBC driver:
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
odbc:DSN=SAMPLE;UID=john;PWD=mypass
</pre></div>
    </div>
    <div class="example-contents"><p>
     The PHP script can then create a database connection by simply
     passing the <em>uri:</em> parameter and pointing to
     the file URI:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Connect&nbsp;to&nbsp;an&nbsp;ODBC&nbsp;database&nbsp;using&nbsp;driver&nbsp;invocation&nbsp;*/<br /></span><span style="color: #0000BB">$dsn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'uri:file:///usr/local/dbconnect'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">PDOException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Connection&nbsp;failed:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-894"><p><strong>Example #3  使用别名创建一个PDO实例 </strong></p>
    <div class="example-contents"><p>
     The following example assumes that <var class="filename">php.ini</var> contains the following
     entry to enable a connection to a MySQL database using only the
     alias <em>mydb</em>:
    </p></div>
    <div class="example-contents"><div class="inicode"><pre class="inicode">[PDO]
pdo.dsn.mydb=&quot;mysql:dbname=testdb;host=localhost&quot;</pre>
</div></div>

    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;&nbsp;使用别名连接到一个ODBC数据库&nbsp;&nbsp;*/<br /></span><span style="color: #0000BB">$dsn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'mydb'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br />try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br />}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">PDOException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Connection&nbsp;failed:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdo.commit.html">PDO::commit</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdo.errorcode.html">PDO::errorCode</a></div>
 <div class="up"><a href="class.pdo.html">PDO</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
